//
//  LCAdInvestViewController.m
//  lechuang
//
//  Created by Mr_hi on 2023/3/29.
//  Copyright © 2023 Mooyi. All rights reserved.
//

#import "LCAdInvestViewController.h"
#import "XWXTextView.h"

#import "LCTaskService.h"

@interface LCAdInvestViewController ()
//项目名称
@property (weak, nonatomic) IBOutlet UITextField *projectTfl;
//产品单价
@property (weak, nonatomic) IBOutlet UITextField *univalenceTfl;
//结算周期
@property (weak, nonatomic) IBOutlet UITextField *settlementTfl;
//联系人姓名
@property (weak, nonatomic) IBOutlet UITextField *nameTfl;
//联系人电话
@property (weak, nonatomic) IBOutlet UITextField *phoneTfl;
//产品说明
@property (weak, nonatomic) IBOutlet XWXTextView *explainTv;

@property (weak, nonatomic) IBOutlet UILabel *countLb;

@end

@implementation LCAdInvestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.explainTv.placeholder = @"请输入产品说明";
    [LCNotiCenter addObserver:self selector:@selector(handleTextChanged) name:UITextViewTextDidChangeNotification object:nil];
}

- (IBAction)backClick:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)handleTextChanged {
    if (!self.explainTv.markedTextRange) {
        if (self.explainTv.text.length > 200) {
            self.explainTv.text = [self.explainTv.text substringToIndex:200];
        }
        self.countLb.text = kFORMAT(@"%ld/200",self.explainTv.text.length);
    }
}

- (IBAction)submitBtnAction:(UIButton *)sender {
    if (!self.projectTfl.text.length) {
        [self showErrorText:self.projectTfl.placeholder];
        return;
    }
    if (!self.univalenceTfl.text.length) {
        [self showErrorText:self.univalenceTfl.placeholder];
        return;
    }
    if (!self.settlementTfl.text.length) {
        [self showErrorText:self.settlementTfl.placeholder];
        return;
    }
    if (!self.nameTfl.text.length) {
        [self showErrorText:self.nameTfl.placeholder];
        return;
    }
    if (!self.phoneTfl.text.length) {
        [self showErrorText:self.phoneTfl.placeholder];
        return;
    }
    if (![NSString isMobileNumber:self.phoneTfl.text]) {
        [self showErrorText:@"请输入正确的手机号"];
        return;
    }
    if (!self.explainTv.text.length) {
        [self showErrorText:self.explainTv.placeholder];
        return;
    }
    
    NSMutableDictionary * other = [NSMutableDictionary dictionary];
    other[@"project"] = self.projectTfl.text;
    other[@"univalence"] = self.univalenceTfl.text;
    other[@"settlement"] = self.settlementTfl.text;
    other[@"surname"] = self.nameTfl.text;
    other[@"phone"] = self.phoneTfl.text;
    other[@"explain"] = self.explainTv.text;
    
    [LCTaskService saveFormWithFormId:@"10000000" Other:other Block:^(BOOL isSuccess) {
        if (isSuccess) {
            [self showSuccessText:@"申请成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
}

@end
